﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using GeckoPhotos.Controllers;
using GeckoPhotos.Models;
using GeckoPhotos.Repository;
using GeckoPhotos.Auth;

namespace GeckoPhotos.Service
{
    public class GeckoMembershipProvider : IMembershipProvider
    {

        private readonly UserServiceLayer _userService;

        public GeckoMembershipProvider() :
            this(null)
        {
        }

        public GeckoMembershipProvider(UserServiceLayer userservice)
        {
            _userService = userservice ?? new UserServiceLayer(new EFUserRepository());
        }

        int IMembershipProvider.MinPasswordLength
        {
            get
            {
                // TODO: implement in app settings or database settings table or something
                return 8;
            }
        }

        bool IMembershipProvider.ValidateUser(string username, string password)
        {
            var result = _userService.GetByUsernameAndPassword(username, password);
            return result != null;
        }

        User IMembershipProvider.RegisterNewUser(string username, string password, string name, string email)
        {
            var result = _userService.CreateUser(username, password, name, email);
            return result;
        }

        bool IMembershipProvider.ChangePassword(string username, string oldPassword, string newPassword)
        {
            var result = _userService.ChangePassword(username, oldPassword, newPassword);
            return result;
        }
    }
}